﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DBTool
{
    using PLAN_TABLE_NAME = System.String; 
    using SELECTED_PATH_FOLDER = System.String;
    using SELECTED_FILE = System.String;

    partial class ParsingManager
    {
        private ParseVerifyManager _parsingVerifyManager;
        private Dictionary<PLAN_TABLE_NAME, TableMeta> _tableMetaList;

        public void Parse( DBConnectInfo connectInfo, Dictionary< SELECTED_PATH_FOLDER, List<SELECTED_FILE> > selectedFileDic )
        {
            DBConnect.Instance.Connect(connectInfo);

            if( false == Convert_OrignalTable_To_ServerTable( selectedFileDic ) )
                return;

            if( false == Config._useDBParsing )
                return;

            Take_ServerTable_Into_DB( selectedFileDic );

            Verify( selectedFileDic );
        }

        private bool Convert_OrignalTable_To_ServerTable( Dictionary< SELECTED_PATH_FOLDER, List<SELECTED_FILE> > selectedFileDic )
        {
            bool result = true;

            foreach( KeyValuePair< SELECTED_PATH_FOLDER, List< SELECTED_FILE > > selectedInfo in selectedFileDic )
            {
                foreach (string filePath in selectedInfo.Value )
                {
                    if( Config._exportFileExt != Path.GetExtension( filePath ) )
                        continue;

                    string fileName = Path.GetFileNameWithoutExtension( filePath );

                    if( false == _tableMetaList.ContainsKey( fileName )  )
                    {
                        MessageBox.Show("Script Meta Not Exist. Check file Name : " + filePath, "Script Meta Not Exist", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        throw new Exception();
                    }

                    bool convertResult = _tableMetaList[fileName].Parse( filePath );

                    if( false == convertResult )
                        result = convertResult;
                }
            }

            return result;
        }

        private void Take_ServerTable_Into_DB(Dictionary< SELECTED_PATH_FOLDER, List<SELECTED_FILE> > selectedFileDic)
        {
            string[] dbTableList = DBConnect.Instance.GetDBTableList();

            if (null == dbTableList)
            {
                MessageBox.Show("DB table Not Exist. Check DB", "DB Table Not Exist", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            foreach( KeyValuePair<SELECTED_PATH_FOLDER, List<SELECTED_FILE>> selectedInfo in selectedFileDic )
            {
                foreach( string selectedFilePath in selectedInfo.Value )
                {
                    string fileName = Path.GetFileNameWithoutExtension( selectedFilePath );

                    string[] parseTableList = _tableMetaList[fileName].GetCreateTableName();

                    foreach( string parsedTableName in parseTableList)
                    {
                        if ( false == dbTableList.Contains( parsedTableName ) )
                        {
                            MessageBox.Show("Table Is Not Exist In DB. : " + parsedTableName, "Not Exist Table", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            continue;
                        }

                        string parsedTablePath = selectedInfo.Key + "/" + Config._parseFolderName + "/" + parsedTableName + Config._exportFileExt;

                        bool insertResult = DBConnectHandler.Instance.TableInsertIntoDBProcess(parsedTablePath);

                        if( false == insertResult )
                            return;
                    }
                }
            }
        }

        private void Verify( Dictionary< SELECTED_PATH_FOLDER, List<SELECTED_FILE> > selectedFileDic )
        {
            foreach( KeyValuePair<SELECTED_PATH_FOLDER, List<SELECTED_FILE>> selectedInfo in selectedFileDic )
            {
                foreach( string selectedFilePath in selectedInfo.Value )
                {
                    string fileName = Path.GetFileNameWithoutExtension(selectedFilePath);

                    if (false == _tableMetaList.ContainsKey(fileName))
                        continue;

                    string[] verifyTableNameList = _tableMetaList[fileName].GetCreateTableName();
                    _parsingVerifyManager.Verify( verifyTableNameList, selectedInfo.Key );
                }
            }
        }
    }
}
